<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function(){
	        	var result = "";
				$("#btnSearch").button({
		            icons: {
		                primary: "ui-icon-search"
		            }
		        }).click(function(){
					if(validateEmpty()){
						result = dateDiff($("#startDate").val(), $("#endDate").val(),"/");
			        	if (!result && $("#startDate").val() != "" && $("#endDate").val() != "") {
			        		alert("วันเริ่มต้นต้องน้อยกว่าวันสิ้นสุดกรุณาระบุใหม่");
			        		$("#endDate").val("");
							return false;
			        	} else {
							$("#searchForm").submit();
						}
					} else {
						return false;
					}
		        });
				/* $("#btnExport").button({
		            icons: {
		                primary: "ui-icon-arrowthickstop-1-s"
		            }
				}).click(function(){
					if(validateEmpty()){
						result = dateDiff($("#startDate").val(), $("#endDate").val(),"/");
			        	if (!result) {
			        		alert("วันเริ่มต้นมากกว่าวันสิ้นสุดกรุณาระบุใหม่");
			        		$("#endDate").val("");
							return false;
			        	} else {
			        		window.location='${pageContext.request.contextPath}/acc6?app=ACC60401&job=export&budgetYear='+$("#budgetYear").val()+'&provinceId='+$("#provinceId").val()+"&startDate="+$("#startDate").val()+"&endDate="+$("#endDate").val()+"&type="+$("#type").val()+"&allocateYear="+$("#allocateYear").val();
			        	}
					}	
				}); */
				
				$("#budgetYear").change(function(){
					if ($("#budgetYear").val() != "") {
						$("#startDate").val("01/10/"+($("#budgetYear").val()-1));
					}else {
						$("#startDate").val("");
					}
				});
		        thaiDatepicker("endDate","#","1");
			});
		</script>
	</head>
	<body>
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="searchForm" id="searchForm" action="${pageContext.request.contextPath}/acc6" method="post">
		<input type="hidden" name="app" id="app" value="ACC60404"/>
		<input type="hidden" name="job" id="job" value="search"/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC60404}</b></th>
			</tr>
			<tr>
				<td class="bold">จังหวัด : </td>
				<td>
					<c:if test="${userInfo.areaLevel =='3'}">
						<c:forEach var="data" items="${userInfo.listUserProvice}">
							${data.provinceName}
							<input type="hidden" name="provinceId" id="provinceId" value="${data.provinceId}">
						</c:forEach>
					</c:if>
					<c:if test="${userInfo.areaLevel=='1' || userInfo.areaLevel=='2'}">
						<select name="provinceId" id="provinceId">
							<option value="">เลือกทั้งหมด</option>
							<c:forEach var="data" items="${userInfo.listUserProvice}">
								<c:if test="${data.provinceId == provinceId}" var="provinceSeleted"/>
								<option value="${data.provinceId}" ${provinceSeleted?'selected':''}>${data.provinceName}</option>
							</c:forEach>
						</select>
					</c:if>
				</td>
			</tr>
			<tr>
				<td class="bold">ปีที่ออกรายงาน : </td>
				<td>
					<select name="budgetYear" id="budgetYear">
						<option value="">----- เลือก -----</option>
						<c:forEach items="${lsBudgetYear}" var="data">
							<c:if test="${budgetYear == data.BUDGET_YEAR}" var="selected"/>
							<option value="${data.BUDGET_YEAR}" ${selected?'selected':''}>${data.BUDGET_YEAR}</option>
						</c:forEach>
					</select>
					<span id="$budgetYear" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td class="bold">วันที่ออกรายงาน : </td>
				<td>
					<input type="text" name="startDate" id="startDate" readonly="readonly" size="12" maxlength="10" onkeypress="return numberAndSlash(event);" onblur="dateFormat(this);" value="${startDate}"/>
					ถึงวันที่
					<input type="text" name="endDate" id="endDate" size="12" maxlength="10" onkeypress="return numberAndSlash(event);" onblur="dateFormat(this);" value="${endDate}"/>
					<span id="#endDate" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td align="center" colspan="2">
					<button type="button" name="btnSearch" id="btnSearch" class="acc67-button"> ค้นหาข้อมูล </button>
					<!-- <button type="button" name="btnExport" id="btnExport" class="acc67-button"> Download </button> -->
				</td>
			</tr>
		</table>
		<br/>
		<c:if test="${!empty lsAllocate}">
			<fieldset>
				<legend> สรุปยอดรวมในแต่ละจังหวัด ตามปีงบประมาณ </legend>
				<br/>
					<b>จังหวัด : ${provinceName}</b>
					<table width="100%" class="acc67-grid ui-widget ui-widget-content" border="0">
						<c:set var="sumIncomeAmountTypeB" value="0"/>
						<c:set var="sumIncomeAmountTypeS" value="0"/>
						<c:set var="sumExpenseAmount" value="0"/>
						<c:set var="sumRemain" value="0"/>
						<c:set var="sumIncomeAmountTypeBAll" value="0"/>
						<c:set var="sumIncomeAmountTypeSAll" value="0"/>
						<c:set var="sumExpenseAmountAll" value="0"/>
						<c:set var="sumRemainAll" value="0"/>
						<c:forEach items="${lsAllocate}" var="data" varStatus="x">
							<c:if test="${data.allocateSeq == '1'}">
								<tr>
									<td>ปีงบประมาณ  : ${data.budgetYear}</td>
								</tr>
								<tr>
									<th align="center" class="ui-state-default">ชื่อแจ้งจัดสรรเงินให้คู่สัญญา</th>
									<th align="center" class="ui-state-default" width="20%">รหัสแจ้งจัดสรรเงินให้คู่สัญญา</th>
									<th align="center" class="ui-state-default" width="7%">ยอดยกมา</th> <!-- B -->
									<th align="center" class="ui-state-default" width="20%">จำนวนเงินที่ได้รับจัดสรร</th><!-- S -->
									<th align="center" class="ui-state-default" width="20%">จำนวนเงินที่จ่ายให้หน่วยบริการ</th><!-- T,C,A -->
									<th align="center" class="ui-state-default" width="7%">คงเหลือ</th> <!--(S+B) - (T,C,A )-->
								</tr>
							</c:if>
							<tr class="grid-tr-${x.count % 2}">
								<td>${data.allocateName}</td>
								<td>${data.allocateCode}</td>
								<td align="right">
									<fmt:formatNumber pattern="#,##0.00" value="${data.sumIncomeAmountTypeB}"/>
								</td>
								<td align="right">
									<fmt:formatNumber pattern="#,##0.00" value="${data.sumIncomeAmountTypeS}"/>
								</td>
								<td align="right">
									<fmt:formatNumber pattern="#,##0.00" value="${data.sumExpenseAmount}"/>
								</td>
								<td align="right">
									<fmt:formatNumber pattern="#,##0.00" value="${(data.sumIncomeAmountTypeB+data.sumIncomeAmountTypeS) - data.sumExpenseAmount}" />
								</td>
								<c:set var="sumIncomeAmountTypeB" value="${data.sumIncomeAmountTypeB + sumIncomeAmountTypeB}"/>
								<c:set var="sumIncomeAmountTypeS" value="${data.sumIncomeAmountTypeS + sumIncomeAmountTypeS}"/>
								<c:set var="sumExpenseAmount" value="${data.sumExpenseAmount + sumExpenseAmount}"/>
								<c:set var="sumRemain" value="${((data.sumIncomeAmountTypeB+data.sumIncomeAmountTypeS) - data.sumExpenseAmount) + sumRemain}"/>
							</tr>
							<c:if test="${data.lastBudgetYear == 'Y'}">
								<tr>
									<td colspan="2" align="right">ยอดรวม </td>
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${sumIncomeAmountTypeB}"/>
									</td>
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${sumIncomeAmountTypeS}"/>
									</td>
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${sumExpenseAmount}"/>
									</td>
									<td align="right">
										<fmt:formatNumber pattern="#,##0.00" value="${sumRemain}"/>
									</td>
								</tr>
								<c:set var="sumIncomeAmountTypeBAll" value="${sumIncomeAmountTypeBAll+sumIncomeAmountTypeB}"/>
								<c:set var="sumIncomeAmountTypeSAll" value="${sumIncomeAmountTypeSAll+sumIncomeAmountTypeS}"/>
								<c:set var="sumExpenseAmountAll" value="${sumExpenseAmountAll+sumExpenseAmount}"/>
								<c:set var="sumRemainAll" value="${sumRemainAll+sumRemain}"/>
								<c:set var="sumIncomeAmountTypeB" value="0"/>
								<c:set var="sumIncomeAmountTypeS" value="0"/>
								<c:set var="sumExpenseAmount" value="0"/>
								<c:set var="sumRemain" value="0"/>
							</c:if>
						</c:forEach>
						
								<tr>
									<td colspan="2" align="right" class="bold">ยอดรวมทั้งหมด </td>
									<td align="right" class="bold">
										<fmt:formatNumber pattern="#,##0.00" value="${sumIncomeAmountTypeBAll}"/>
									</td>
									<td align="right" class="bold">
										<fmt:formatNumber pattern="#,##0.00" value="${sumIncomeAmountTypeSAll}"/>
									</td>
									<td align="right" class="bold">
										<fmt:formatNumber pattern="#,##0.00" value="${sumExpenseAmountAll}"/>
									</td>
									<td align="right" class="bold">
										<fmt:formatNumber pattern="#,##0.00" value="${sumRemainAll}"/>
									</td>
								</tr>
					</table>
				</fieldset>
			</c:if>
		</form>
	</body>
</html>